# -*- coding: utf-8 -*-

import sqlite3
import time

class StatisticsHolder:
    def __init__(self):
        #self.countHighTeachers()
        #self.countCategoryTeachers()
        #self.countDegreeTeachers()
        #self.listHighTeachers()
        #self.listCategoryTeachers()
        #self.listDegreeTeachers()
        pass

    def countHighTeachers(self):
        t1 = time.clock()
        conn = sqlite3.connect("TP1.db")
        c = conn.cursor()

        stats = {}

        for i in range(0, 11):
            c.execute('''select * from reb{0}'''.format(i))
            d = {}
            x = 0

            for row in c: 
                if (row[8] in d):
                    d[row[8]] = d[row[8]] + 1
                    pass
                else:
                    d[row[8]] = 1
                    pass
                pass
            
            if (i < 10):
                stats["200{0}".format(i)] = d
                pass
            else:
                stats["20{0}".format(i)] = d
                pass
            pass

        conn.close()

        #print stats
        t2 = time.clock()
        print "Count High teachers = ", t2 - t1
        return stats

    def countCategoryTeachers(self):
        t1 = time.clock()
        conn = sqlite3.connect("TP1.db")
        c = conn.cursor()
        
        stats = {}

        for i in range(0, 11):
            c.execute('''select * from reb{0}'''.format(i))
            d = {}
            x = 0

            for row in c:
                if (row[8] in d):
                    if (row[4] in d[row[8]]):
                        d[row[8]][row[4]] = d[row[8]][row[4]] + 1
                        pass
                    else:
                        d[row[8]][row[4]] = 1
                        pass
                    pass
                else:
                    d[row[8]] = {row[4]: 1}
                    pass
                pass
            
            if (i < 10):
                stats["200{0}".format(i)] = d
                pass
            else:
                stats["20{0}".format(i)] = d
                pass
            pass

        conn.close()

        # print stats
        t2 = time.clock()
        print "Count Category Teachers = ", t2 - t1
        return stats

    def countDegreeTeachers(self):
        t1 = time.clock()
        conn = sqlite3.connect("TP1.db")
        c = conn.cursor()
        
        stats = {}

        for i in range(0, 11):
            c.execute('''select * from reb{0}'''.format(i))
            d = {}
            x = 0

            for row in c:                
                if (row[8] in d):
                    if (row[2] in d[row[8]]):
                        d[row[8]][row[2]] = d[row[8]][row[2]] + 1
                        pass
                    else:
                        if ((row[2] == "Licenciatura") or \
                            (row[2] == "Mestrado") or \
                            (row[2] == "Doutoramento")):
                            d[row[8]][row[2]] = 1
                            pass
                        pass
                    pass
                else:
                    if ((row[2] == "Licenciatura") or \
                        (row[2] == "Mestrado") or \
                        (row[2] == "Doutoramento")):                    
                        d[row[8]] = {row[2]: 1}
                        pass
                    pass
                pass
            
            if (i < 10):
                stats["200{0}".format(i)] = d
                pass
            else:
                stats["20{0}".format(i)] = d
                pass
            pass

        conn.close()

        #print stats
        t2 = time.clock()
        print "Count Degree Teachers = ", t2 - t1        
        return stats

    def listHighTeachers(self):
        t1 = time.clock()
        conn = sqlite3.connect("TP1.db")
        c = conn.cursor()

        stats = {}

        for i in range(0, 11):
            c.execute('''select * from reb{0}'''.format(i))
            d = {}
            x = 0

            for row in c:
                if (row[8] in d):
                    d[row[8]].append(row[1])
                    pass
                else:
                    d[row[8]] = [row[1]]
                    pass
                pass
            
            if (i < 10):
                stats["200{0}".format(i)] = d
                pass
            else:
                stats["20{0}".format(i)] = d
                pass
            pass

        conn.close()

        # print stats
        # print stats.get("2000").get("Escola Naval").items()
        t2 = time.clock()
        print "List High Teachers = ", t2 - t1
        return stats

    def listCategoryTeachers(self):
        t1 = time.clock()
        conn = sqlite3.connect("TP1.db")
        c = conn.cursor()
        
        stats = {}

        for i in range(0, 11):
            c.execute('''select * from reb{0}'''.format(i))
            d = {}
            x = 0

            for row in c:
                if (row[8] in d):
                    if (row[4] in d[row[8]]):
                        d[row[8]][row[4]].append(row[1])
                        pass
                    else:
                        d[row[8]][row[4]] = [row[1]]
                        pass
                    pass
                else:
                    d[row[8]] = {row[4]: [row[1]]}
                    pass
                pass
            
            if (i < 10):
                stats["200{0}".format(i)] = d
                pass
            else:
                stats["20{0}".format(i)] = d
                pass
            pass

        conn.close()

        # print stats
        # print stats.get("2000").get("Escola Naval").items()
        t2 = time.clock()
        print "List Category Teachers = ", t2 - t1
        return stats

    def listDegreeTeachers(self):
        t1 = time.clock()
        conn = sqlite3.connect("TP1.db")
        c = conn.cursor()
        
        stats = {}

        for i in range(0, 11):
            c.execute('''select * from reb{0}'''.format(i))
            d = {}
            x = 0

            for row in c:                
                if (row[8] in d):
                    if (row[2] in d[row[8]]):
                        d[row[8]][row[2]].append(row[1])
                        pass
                    else:
                        if ((row[2] == "Licenciatura") or \
                            (row[2] == "Mestrado") or \
                            (row[2] == "Doutoramento")):
                            d[row[8]][row[2]] = [row[1]]
                            pass
                        pass
                    pass
                else:
                    if ((row[2] == "Licenciatura") or \
                        (row[2] == "Mestrado") or \
                        (row[2] == "Doutoramento")):                    
                        d[row[8]] = {row[2]: [row[1]]}
                        pass
                    pass
                pass
            
            if (i < 10):
                stats["200{0}".format(i)] = d
                pass
            else:
                stats["20{0}".format(i)] = d
                pass
            pass

        conn.close()

        #print stats
        #print stats.get("2000").get("Escola Naval").items()
        t2 = time.clock()
        print "List Degree Teachers = ", t2 - t1   
        return stats
    
##stats = StatisticsHolder()
